using System;
using System.Data;
using System.IO;
using WP.DB;

namespace WP.Commons
{
	/// <summary>
	/// Summary description for Log.
	/// </summary>
	public class Log
	{
		public Log()
		{
			//
			// TODO: Add constructor logic here
			//
		}

      public static void Warning(SystemModules systemModules,string strMessage)
      {
         WriteAnyLog(MessageLogType.Warning,systemModules,strMessage);
      }

      public static void Prompt(SystemModules systemModules,string strMessage)
      {
         WriteAnyLog(MessageLogType.Prompt,systemModules,strMessage);
      }

      public static void Error(SystemModules systemModules,string strMessage)
      {
         WriteAnyLog(MessageLogType.Error,systemModules,strMessage);
      }


		public static void WriteAnyLog(MessageLogType messageLogType,SystemModules systemModules,string strMessage)
		{			
			if (SysVar.IsWriteMessageLog)
			{
				string strPath ="";
				string mstrFilePath="";
				string strTitle="";

				strTitle=DateTime.Now.ToString();
				strTitle+="  "+SysVar.UserName;
				strTitle+="  "+messageLogType.ToString();
				strTitle+="  "+systemModules.ToString();
           
				strPath="MsgLog"+DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString().PadLeft(2,'0')+DateTime.Now.Day.ToString().PadLeft(2,'0')+".txt";
				mstrFilePath=SysVar.MessageLogFilePath;

				if (mstrFilePath!="")
				{  
					if(mstrFilePath.Substring(mstrFilePath.Length-1,1)=="\\")
					{
						strPath=mstrFilePath+strPath;
					}
					else
					{
						strPath=mstrFilePath+ "\\"+strPath;
					}
				}

				if (!File.Exists(strPath)) 
				{
					using (StreamWriter sw = File.CreateText(strPath))
					{
						sw.WriteLine("Message Log: ");			
					}
				}
         
				using (StreamWriter sw = File.AppendText(strPath)) 
				{
					sw.WriteLine(strTitle + "\n" + strMessage+"\n");
				}
			}
		}

		public static void WriteDataLog(DBAccess dBAccess,string strSql)
		{
			WriteDataLog (dBAccess.GetDataSet(strSql).Tables[0].DefaultView);
		}

		public static void WriteDataLog(DataView dv)
		{			
			if (SysVar.IsWriteMessageLog)
			{
				string strPath ="";
				string mstrFilePath="";
				string strMsg="";
				DataTable dt=dv.Table;

				for(int i=0;i<dt.Columns.Count;i++)
				{
					strMsg+=dt.Columns[i].ColumnName+"|";
				}
				strMsg+="\n";
				foreach(DataRowView drv in dv)
				{
					for(int i=0;i<dt.Columns.Count;i++)
					{
						strMsg+=drv[i].ToString()+"|";
					}
					strMsg+="\n";
				}

           
				strPath="MsgLog"+DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString().PadLeft(2,'0')+DateTime.Now.Day.ToString().PadLeft(2,'0')+".txt";
				mstrFilePath=SysVar.MessageLogFilePath;

				if (mstrFilePath!="")
				{  
					if(mstrFilePath.Substring(mstrFilePath.Length-1,1)=="\\")
					{
						strPath=mstrFilePath+strPath;
					}
					else
					{
						strPath=mstrFilePath+ "\\"+strPath;
					}
				}

				if (!File.Exists(strPath)) 
				{
					using (StreamWriter sw = File.CreateText(strPath))
					{
						sw.WriteLine("Message Log: ");			
					}
				}
         
				using (StreamWriter sw = File.AppendText(strPath)) 
				{
					sw.WriteLine(strMsg+"\n");
				}
			}
		}

	}
}
